package com.hongfasoft.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.hongfasoft.util.DBManager;
import com.hongfasoft.util.MessageUtil;
import com.hongfasoft.wx.message.Article;
import com.hongfasoft.wx.message.NewsMessage;
import com.hongfasoft.wx.message.TextMessage;

public class SendPicArticl
{
	public static String responseAsPic(Map<String, String> requestMap, String menu)
	{
		String fromUserName = requestMap.get("FromUserName");
		boolean isMemberRight = DBManager.checkIsMember(fromUserName);
		return responseAsPic(requestMap, isMemberRight, menu);
	}

	public static String responseAsPic(Map<String, String> requestMap, boolean isMemberRight, String menu)
	{
		String respMessage = null;
		// 发送方帐号（open_id）
		String fromUserName = requestMap.get("FromUserName");
		// 公众帐号
		String toUserName = requestMap.get("ToUserName");

		// 默认回复此文本消息
		TextMessage textMessage = new TextMessage();
		textMessage.setToUserName(fromUserName);
		textMessage.setFromUserName(toUserName);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		textMessage.setFuncFlag(0);

		// 创建图文消息
		NewsMessage newsMessage = new NewsMessage();
		newsMessage.setToUserName(fromUserName);
		newsMessage.setFromUserName(toUserName);
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
		newsMessage.setFuncFlag(0);

		List<Article> articleList = null;

		if ("zhenduan".equalsIgnoreCase(menu))
		{
			String content = requestMap.get("Content").trim();

			String usercontent = content.replaceAll("^诊断[\\+ ~!@#%^-_=]?", "");
			String stockname = "", stockcode = "";
			System.out.println("stockcode=" + content);
			if (StringUtils.isEmpty(usercontent))
			{
				// 由于href属性值必须用双引号引起，这与字符串本身的双引号冲突，所以要转义
				textMessage.setContent(getUseage());
				// 将文本消息对象转换成xml字符串
				respMessage = MessageUtil.textMessageToXml(textMessage);
			} else
			{
				try
				{
					Integer.valueOf(usercontent);
					stockcode = usercontent;
				} catch (Exception e)
				{
					stockname = usercontent;
				}
				articleList = DBManager.stockArticleList(fromUserName, isMemberRight, stockcode, stockname, menu);
				System.out.println("诊断" + stockname + articleList.size());
				if (articleList == null || articleList.size() == 0)
				{
					// 由于href属性值必须用双引号引起，这与字符串本身的双引号冲突，所以要转义
					textMessage.setContent("您输入的股票：\n" + usercontent + "\n未找到对应分析文章。");
					// 将文本消息对象转换成xml字符串
					respMessage = MessageUtil.textMessageToXml(textMessage);
					// return respMessage;
				} else
				{
					newsMessage.setArticleCount(articleList.size());
					newsMessage.setArticles(articleList);
					respMessage = MessageUtil.newsMessageToXml(newsMessage);
				}
			}
		} else
		{
			articleList = DBManager.articleList(fromUserName, isMemberRight, menu);
			if (articleList != null)
			{
				newsMessage.setArticleCount(articleList.size());
				newsMessage.setArticles(articleList);
				respMessage = MessageUtil.newsMessageToXml(newsMessage);
			}
		}

		if (articleList == null || articleList.size() == 0)
		{
			textMessage.setContent("功能完善中，敬请期待");
			respMessage = MessageUtil.textMessageToXml(textMessage);
		}
		return respMessage;
	}

	/**
	 * emoji表情转换(hex -> utf-16)
	 * 
	 * @param hexEmoji
	 * @return
	 */
	public static String emoji(int hexEmoji)
	{
		return String.valueOf(Character.toChars(hexEmoji));
	}

	public static String getUseage()
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("股票诊断操作指南").append("\n\n");
		buffer.append("回复：诊断+股票代码/股票名称").append("\n");
		buffer.append("例如：诊断600000").append("\n");
		buffer.append("例如：诊断浦发银行").append("\n");
		return buffer.toString();

	}
}
